package com.selvn.db;

import com.selvn.encryptnote.Config;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBOpenHelper extends SQLiteOpenHelper {
	private static final int VERSION = 3;
	
	public DBOpenHelper( Context context )
	{
		this(context, Config.DBNAME, null, VERSION );
	}
	public DBOpenHelper( Context context, String dbName, CursorFactory factory, int dbVersion)
	{
		super(context, dbName, factory, dbVersion);
	}
	
	@Override
	public void onCreate( SQLiteDatabase db )
	{
		Log.i(Config.LOGTAG, "============= start database create ===");
		String sql = "create table IF NOT EXISTS "+ Config.DBTABLE +" ( "+ Config.DBID +" integer primary key autoincrement, "
                + Config.DBTITLE + " text not null, " + Config.DBBODY + " text not null, " 
                + Config.DBCREATEDATE + " text not null, "+ Config.DBMODIFY +" text not null, "
                + Config.DBLOCATION1 + " text null, "+ Config.DBLOCATION2 + " text null );";
		db.execSQL( sql );
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.i(Config.LOGTAG, "Upgrade database from old version " + Integer.toString( oldVersion ) + " to " + Integer.toString( newVersion ));
        // 注：生产环境上不能做删除操作
        // db.execSQL("DROP TABLE IF EXISTS PERSON"); 
		// If you need to add a column
	    if ( oldVersion == 2 ) {
	    	Log.i(Config.LOGTAG, "Old version " + Integer.toString( oldVersion ) + " start upgrading...");
	        db.execSQL("ALTER TABLE "+Config.DBTABLE+" ADD COLUMN "+Config.DBLOCATION1+" text null;");
	        db.execSQL("ALTER TABLE "+Config.DBTABLE+" ADD COLUMN "+Config.DBLOCATION2+" text null;");
	    }
        //onCreate(db);
	}	
}
